<?php
class Zendvn_Filter_Filter implements Zend_Filter_Interface{
	
	public function filter($value){
		$filter = new Zend_Filter();
		$filter->addFilter(new Zend_Filter_StringToLower(array('encoding'=>'utf-8')))
				->addFilter(new Zendvn_Filter_Utf8())
				->addFilter(new Zend_Filter_Alnum(true))
				->addFilter(new Zend_Filter_PregReplace('#\s+#','-'));
		$value = $filter->filter($value);
		return $value;
	}
	
	public function specialCharacter($value){
		$find[] = 'Ã¢â‚¬Å“';  // left side double smart quote
		$find[] = 'Ã¢â‚¬Â�';  // right side double smart quote
		$find[] = 'Ã¢â‚¬Ëœ';  // left side single smart quote
		$find[] = 'Ã¢â‚¬â„¢';  // right side single smart quote
		$find[] = 'Ã¢â‚¬Â¦';  // elipsis
		$find[] = 'Ã¢â‚¬â€�';  // em dash
		$find[] = 'Ã¢â‚¬â€œ';  // en dash
		$find[] = 'Ã‚â‚¬Å“';  // left side double smart quote
		$find[] = 'Ã‚â‚¬Â�';  // right side double smart quote
		$find[] = 'Ã‚â‚¬Ëœ';  // left side single smart quote
		$find[] = 'Ã‚â‚¬â„¢';  // right side single smart quote
		$find[] = 'Ã‚â‚¬Â¦';  // elipsis
		$find[] = 'Ã‚â‚¬â€�';  // em dash
		$find[] = 'Ã‚â‚¬â€œ';  // en dash
		
		$replace[] = '"';
		$replace[] = '"';
		$replace[] = "'";
		$replace[] = "'";
		$replace[] = "...";
		$replace[] = "-";
		$replace[] = "-";
		
		$value = str_replace($find, $replace, $value);
		return $value;
	}
	
	public function replaceSpecial($str){
		$chunked = str_split($str,1);
		$str = "";
		foreach($chunked as $chunk){
			$num = ord($chunk);
			// Remove non-ascii & non html characters
			if ($num >= 32 && $num <= 123){
				$str.=$chunk;
			}
		}
		return $str;
	}
	
	public static function filterContent($str, $options = null){
		if($options == null){
			$str = str_replace('\\','',$str);
			$str = str_replace('\\\'','"',$str);
			$str = str_replace('\\\\','',$str);
			$str = str_replace('<pre','<div',$str);
			$str = str_replace('</pre','</div',$str);
		}
		if($options == 'filter'){
			$str = str_replace('\\','',$str);
			$str = str_replace('\\\'','"',$str);
			$str = str_replace('\\\\','',$str);
			$str = str_replace('<pre','<div',$str);
			$str = str_replace('</pre','</div',$str);
			$str = str_replace('<script>','&lt;script&gt;',$str);
			$str = str_replace('<script type="text/javascript">','&lt;script type="text/javascript"&gt;',$str);
			$str = str_replace('</script>','&lt;/script&gt;',$str);
			$str = str_replace('<javascript>','&lt;javascript&gt;',$str);
			$str = str_replace('</javascript>','&lt;/javascript&gt;',$str);
			$str = str_replace('<?','&lt;?',$str);
			$str = str_replace('<?php','&lt;?php',$str);
			$str = str_replace('?>','?&gt;',$str);
		}
		
		return $str;
	}
	
	/**
	 * Rut ngan noi dung bai viet
	 * @input:	Cong hoa xa hoi chu nghia VN, 4
	 * @output:	Cong...
	 * @param unknown_type $str
	 * @param unknown_type $len
	 * @param unknown_type $charset
	 */
	public static function shortString($str, $len = 200, $charset='UTF-8'){
		$str = html_entity_decode($str, ENT_QUOTES, $charset);
		$str = strip_tags($str);
		if(mb_strlen($str, $charset)> $len){
			$arr = explode(' ', $str);
			$str = substr($str, 0, $len);
			$arrRes = explode(' ', $str);
			$last = $arr[count($arrRes)-1];
			unset($arr);
			if(strcasecmp($arrRes[count($arrRes)-1], $last)){
				unset($arrRes[count($arrRes)-1]);
			}
			return implode(' ', $arrRes)."...";
		}
		return $str;
	}
	
	public static function cutString($str, $len = 200, $charset='UTF-8'){
		$str = html_entity_decode($str, ENT_QUOTES, $charset);
		if(mb_strlen($str, $charset)> $len){
			$str = substr($str, 0, $len) . '...';
		}
		return $str;
	}
	
}